package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysPowers;
import com.ruoyi.system.domain.SysTable;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysTableService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysSortMapper;
import com.ruoyi.system.domain.SysSort;
import com.ruoyi.system.service.ISysSortService;

/**
 * 分类管理Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-15
 */
@Service
public class SysSortServiceImpl implements ISysSortService
{
    @Autowired
    private SysSortMapper sysSortMapper;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysTableService sysTableService;

    /**
     * 查询分类管理
     *
     * @param recNo 分类管理ID
     * @return 分类管理
     */
    @Override
    public SysSort selectSysSortById(String recNo)
    {
        SysSort sysSort = sysSortMapper.selectSysSortById(recNo);
        sysSort.setSortPrantName(sysSortMapper.selectSysSortById(sysSort.getSortPrantNo()).getSortName());
        return sysSort;
    }

    /**
     * 查询分类管理列表
     *
     * @param sysSort 分类管理
     * @return 分类管理
     */
    @Override
    public List<SysSort> selectSysSortList(SysSort sysSort)
    {
        List<SysSort> sysSorts = sysSortMapper.selectSysSortList(sysSort);
        return TableName(sysSorts);
    }

    @Override
    public List<SysSort> selectSysSortLists(SysSort sysSort)
    {
        List<SysSort> sysSorts = sysSortMapper.selectSysSortList(sysSort);
        //调用改造list成树形结构数据
        List<SysSort> sysSorts1 = buildTree(sysSorts,"1");
        return sysSorts1;
    }

    @Override
    public List querylList(SysSort sysSort) {
        //判断是否是底层分类
        if (sysSort.getIsLast().equals("1")) {
            //不是底层分类展示对应的分类数据
            SysSort sysSort1 = new SysSort();
            sysSort1.setIsLast(sysSort.getIsLast());
            sysSort1.setSortPrantNo(sysSort.getSortPrantNo());
            List<SysSort> sysSorts = selectSysSortList(sysSort1);
            return sysSorts;
        }else {
            //是底层分类展示对应的表数据
            SysTable sysTable = new SysTable();
            sysTable.setSortNo(sysSort.getSortPrantNo());
            List<SysTable> sysTables = sysTableService.selectSysTableList(sysTable);
            for (SysTable table : sysTables) {
                table.setSortName(sysSort.getSortName());
            }
            return sysTables;
        }
    }

    /**
     * 新增分类管理
     *
     * @param sysSort 分类管理
     * @return 结果
     */
    @Override
    public int insertSysSort(SysSort sysSort)
    {
        //获取用户名和id
        SysUser sysUser = userService.selectUserByUserName(SecurityUtils.getUsername());
        SysDept sysDept = deptService.selectDeptById(Long.parseLong(sysUser.getDeptId()));
        sysSort.setCreateBy(sysDept.getDeptName());
        sysSort.setCreateTime(DateUtils.getNowDate());
        //获取uuid
        UUID uuid=UUID.randomUUID();
        //去掉uuid中间的下划线
        String[] idd=uuid.toString().split("-");
        String id=idd[0]+idd[1]+idd[2]+idd[3]+ idd[4];
        sysSort.setRecNo(id);
        return sysSortMapper.insertSysSort(sysSort);
    }

    /**
     * 修改分类管理
     *
     * @param sysSort 分类管理
     * @return 结果
     */
    @Override
    public int updateSysSort(SysSort sysSort)
    {
        //获取用户名和id
        SysUser sysUser = userService.selectUserByUserName(SecurityUtils.getUsername());
        SysDept sysDept = deptService.selectDeptById(Long.parseLong(sysUser.getDeptId()));
        sysSort.setUpdateBy(sysDept.getDeptName());
        sysSort.setUpdateTime(DateUtils.getNowDate());
        return sysSortMapper.updateSysSort(sysSort);
    }

    /**
     * 批量删除分类管理
     *
     * @param recNos 需要删除的分类管理ID
     * @return 结果
     */
    @Override
    public int deleteSysSortByIds(String[] recNos)
    {
        int i=0;
        for (String recNo : recNos) {
            SysSort sysSort1 = new SysSort();
            sysSort1.setSortPrantNo(recNo);
            //判断是否有下级分类
            if (sysSortMapper.selectSysSortList(sysSort1).size()!=0) {
                return -1;
            }
            //获取用户名和id
            SysUser sysUser = userService.selectUserByUserName(SecurityUtils.getUsername());
            SysDept sysDept = deptService.selectDeptById(Long.parseLong(sysUser.getDeptId()));
            SysSort sysSort = new SysSort();
            sysSort.setUpdateBy(sysDept.getDeptName());
            sysSort.setUpdateTime(DateUtils.getNowDate());
            sysSort.setRecNo(recNo);
            sysSortMapper.deleteSysSortById(sysSort);
            i++;
        }
        return i;
    }

    /**
     * 删除分类管理信息
     *
     * @param recNo 分类管理ID
     * @return 结果
     */
    @Override
    public int deleteSysSortById(String recNo)
    {
        //获取用户名和id
        SysUser sysUser = userService.selectUserByUserName(SecurityUtils.getUsername());
        SysDept sysDept = deptService.selectDeptById(Long.parseLong(sysUser.getDeptId()));
        SysSort sysSort = new SysSort();
        sysSort.setUpdateBy(sysDept.getDeptName());
        return sysSortMapper.deleteSysSortById(sysSort);
    }

    //制造树形结构数据
    private static List<SysSort> buildTree(List<SysSort> sysSortList,String pid) {
        List<SysSort> treeList = new ArrayList<SysSort>();
        for (SysSort sysSort : sysSortList) {
            if (sysSort.getSortPrantNo().equals(pid)) {
                sysSort.setLabel(sysSort.getSortName());
                sysSort.setChildren(buildTree(sysSortList, sysSort.getRecNo()));
                treeList.add(sysSort);
            }
        }
        return treeList;
    }
    //制造前端需要展示的数据
    public List<SysSort> TableName(List<SysSort> sysSort) {
        for (SysSort sort : sysSort) {
            if (sort.getSortMainTableNo() != null) {
                boolean contains = sort.getSortMainTableNo().contains(",");
                String[] split ;
                if (contains){
                    split = sort.getSortMainTableNo().split(",");
                }else {
                    String[] strings = new String[1];
                    strings[0]= sort.getSortMainTableNo();
                    split= strings;
                }
                SysTable sysTable = new SysTable();
                sysTable.setRecNoS(split);
                List<SysTable> sysTables = sysTableService.selectSysTableNameList(sysTable);
                for (SysTable table : sysTables) {
                    if (sort.getSortMainTableName()!=null){
                        sort.setSortMainTableName(sort.getSortMainTableName() + "," + table.getTableName());
                    }else {
                        sort.setSortMainTableName(table.getTableName());
                    }
                }
            }
            if (sysTableService.selectSysTableById(sort.getSortPrantNo()) != null) {
                sort.setSortPrantName(sysTableService.selectSysTableById(sort.getSortPrantNo()).getTableName());
            }
            if (sort.getSortViewTableNo() != null) {
                boolean contains = sort.getSortViewTableNo().contains(",");
                String[] split ;
                if (contains){
                    split = sort.getSortViewTableNo().split(",");
                }else {
                    String[] strings = new String[1];
                    strings[0]= sort.getSortViewTableNo();
                    split= strings;
                }
                SysTable sysTable = new SysTable();
                sysTable.setRecNoS(split);
                List<SysTable> sysTables = sysTableService.selectSysTableNameList(sysTable);
                for (SysTable table : sysTables) {
                    if (sort.getSortViewTableName()!=null){
                        sort.setSortViewTableName(sort.getSortViewTableName() + "," + table.getTableName());
                    }else {
                        sort.setSortViewTableName(table.getTableName());
                    }
                }
            }
            if (sort.getSortSublist() != null) {
                boolean contains = sort.getSortSublist().contains(",");
                String[] split ;
                if (contains){
                    split = sort.getSortSublist().split(",");
                }else {
                    String[] strings = new String[1];
                    strings[0]= sort.getSortSublist();
                    split= strings;
                }
                SysTable sysTable = new SysTable();
                sysTable.setRecNoS(split);
                List<SysTable> sysTables = sysTableService.selectSysTableNameList(sysTable);
                for (SysTable table : sysTables) {
                    if (sort.getSortSublistName()!=null){
                        sort.setSortSublistName(sort.getSortSublistName() + "," + table.getTableName());
                    }else {
                        sort.setSortSublistName(table.getTableName());
                    }
                }
            }

        }
        return sysSort;
    }
}
